#pragma once

#include "Factory.h"

template <typename T>
class FactoryManager
{
public:
	virtual ~FactoryManager()
	{
		clear();
	}

	bool add_factory(T* val)
	{
		FACTORY_MAP::iterator iter = 
			FactoryMap.insert(
				make_pair(val->signature(), val)
			);
		return iter->second;
	}
	bool remove_factory(const std::string& signature)
	{
		FACTORY_MAP::iterator iter = 
			FactoryMap.find(signature);
		if (iter != FactoryMap.end())
		{
			FactoryMap.erase(iter);
			return true;
		}
		else
		{
			return false;
		}
	}

	typename T::value_type* create_instance(const std::string& signature) const
	{
		FACTORY_MAP::iterator iter = 
			FactoryMap.find(signature);
		if (iter != FactoryMap.end())
		{
			return iter->create_instance();
		}
		else
		{
			return NULL;
		}
	}

	void clear()
	{
		for (FACTORY_MAP::iterator iter = FactoryMap.begin();
			iter != FactoryMap.end();
			++iter)
		{
			delete iter->second;
		}
		FactoryMap.clear();
	}
private:
	typedef std::map<std::string, T*> FACTORY_MAP;
	FACTORY_MAP FactoryMap;
};
